<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="服务商名称" prop="provName">
        <el-input
          v-model="queryParams.provName"
          placeholder="请输入服务商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户服务区" prop="provRegion">
        <el-input
          v-model="queryParams.provRegion"
          placeholder="请输入商户服务区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户地址" prop="provAddr">
        <el-input
          v-model="queryParams.provAddr"
          placeholder="请输入商户地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系人" prop="liaison">
        <el-input
          v-model="queryParams.liaison"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抽成比例" prop="drawProportion">
        <el-input
          v-model="queryParams.drawProportion"
          placeholder="请输入抽成比例  例如：0.05"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入登录账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="服务类目 编号  逗号隔开," prop="servCategory">
				<el-input v-model="queryParams.servCategory" placeholder="请输入服务类目 编号  逗号隔开," clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item> -->
      <el-form-item label="是否禁用" prop="isDis">
        <el-select v-model="queryParams.isDis" placeholder="请选择">
          <el-option
            v-for="item in [
              { value: '1', label: '正常' },
              { value: '2', label: '禁用' },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:provider:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:provider:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:provider:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:provider:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="providerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务商编号" align="center" prop="id" />
      <el-table-column label="服务商名称" align="center" prop="provName" />
      <el-table-column label="商户服务区" align="center" prop="provRegion" />
      <el-table-column label="商户地址" align="center" prop="provAddr" />
      <el-table-column label="联系人" align="center" prop="liaison" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="抽成比例" align="center" prop="drawProportion" />
      <el-table-column label="登录账号" align="center" prop="account" />
      <!-- <el-table-column label="登录密码" align="center" prop="password" /> -->
      <el-table-column label="营业执照" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.businessLicense"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="资格证书" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.certificateCompetence"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="店铺图片" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.storeIcon"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.servType == 1">家政</el-tag>
          <el-tag type="warning" v-if="scope.row.servType == 2">开锁</el-tag>
          <el-tag type="success" v-if="scope.row.servType == 3">团购</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="服务类目 编号  逗号隔开," align="center" prop="servCategory" />
			<el-table-column label="服务商品编号 逗号隔开," align="center" prop="servGoods" /> -->
      <!-- <el-table-column label="是否删除 0正常 1删除" align="center" prop="isDel" /> -->
      <el-table-column label="是否禁用" align="center" prop="isDis">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isDis == 1">已启用</el-tag>
          <el-tag type="info" v-if="scope.row.isDis == 2">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:provider:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:provider:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="服务商名称" prop="provName">
          <el-input v-model="form.provName" placeholder="请输入服务商名称" />
        </el-form-item>
        <el-form-item label="服务类型" prop="servType" required>
          <el-radio-group v-model="form.servType">
            <el-radio :label="1">家政</el-radio>
            <el-radio :label="2">开锁</el-radio>
            <el-radio :label="3">团购</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="结算类型" prop="settleType" required>
          <el-radio-group v-model="form.settleType" @input="changeSettletype">
            <el-radio :label="1">按月结算</el-radio>
            <el-radio :label="2">周期结算</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="结算周期" prop="settleCycle" v-if="settleTypeShow">
          <el-input
            v-model="form.settleCycle"
            placeholder="请输入结算周期"
            style="width: 80%"
          />
          天
        </el-form-item>
        <el-form-item
          label="结算开始时间"
          prop="settleTime"
          v-if="settleTypeShow"
        >
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.settleTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="商户服务区" prop="provRegion">
          <el-input v-model="form.provRegion" placeholder="请输入商户服务区" />
        </el-form-item>
        <el-form-item label="商户地址" prop="provAddr">
          <el-input v-model="form.provAddr" placeholder="请输入商户地址" />
        </el-form-item>
        <el-form-item
          label="经纬度"
          class="is-required"
          prop="longitudeAndLatitude"
        >
          <el-input
            v-model.trim="longitudeAndLatitude"
            placeholder="请输入经纬度"
          />
          <el-button class="mt10" type="primary" @click="openDialog = true"
            >选择经纬度</el-button
          >
        </el-form-item>
        <el-form-item label="联系人" prop="liaison">
          <el-input v-model="form.liaison" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="抽成比例" prop="drawProportion">
          <el-input
            v-model="form.drawProportion"
            placeholder="请输入抽成比例  例如：0.05"
          />
        </el-form-item>
        <el-form-item label="登录账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="form.password" placeholder="请输入登录密码" />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <image-upload v-model="form.businessLicense" :limit="1" />
        </el-form-item>
        <el-form-item label="资格证书" prop="certificateCompetence">
          <image-upload v-model="form.certificateCompetence" :limit="1" />
        </el-form-item>
        <el-form-item label="店铺图片" prop="storeIcon">
          <image-upload v-model="form.storeIcon" :limit="1" />
        </el-form-item>
        <el-form-item label="店铺详情banner" prop="logo">
          <image-upload v-model="form.logo" :limit="5" />
        </el-form-item>
        <el-form-item
          label="服务类目"
          prop="servCategory"
          v-if="form.servType == 1"
        >
          <el-select
            v-model="form.servCategory"
            multiple
            filterable
            placeholder="请选择服务类目"
            style="width: 100%"
            @change="servCategorychange"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="String(item.id)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="服务商品编号"
          prop="servGoods"
          v-if="form.servType == 1"
        >
          <!-- <el-input v-model="form.servGoods" type="textarea" placeholder="请输入内容" /> -->
          <el-select
            v-model="form.servGoods"
            multiple
            filterable
            placeholder="请选择服务"
            style="width: 100%"
          >
            <el-option
              v-for="item in homeList"
              :key="item.id"
              :label="item.title"
              :value="String(item.id)"
            >
              <span>{{ item.id }}</span>
              <img
                class="ml10"
                style="width: 20px; height: 20px; vertical-align: middle"
                :src="api_host + item.icon"
                alt=""
              />
              <span class="ml10">{{ item.title }}</span>
              <span class="ml10 orange">￥{{ item.price }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否删除" prop="isDel">
					<el-switch v-model="form.isDel" active-color="#13ce66" :active-value="0" :inactive-value="1"
						inactive-color="#999">
					</el-switch>
				</el-form-item> -->
        <el-form-item label="是否禁用" prop="isDis">
          <el-switch
            v-model="form.isDis"
            active-color="#13ce66"
            :active-value="2"
            :inactive-value="1"
            inactive-color="#999"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 经纬度 -->
    <el-dialog
      title="获取经纬度"
      :visible.sync="openDialog"
      append-to-body
      width="80%"
    >
      <iframe
        :src="'https://lbs.qq.com/getPoint/'"
        width="100%"
        height="750px"
        frameBorder="0"
      ></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProvider,
  getProvider,
  delProvider,
  addProvider,
  updateProvider,
} from "@/api/system/provider";
import { listType } from "@/api/system/type";
import { getProductbyids } from "@/api/system/product";
export default {
  name: "Provider",
  data() {
    var validateLongitude = (rule, value, callback) => {
      if (!this.longitudeAndLatitude) {
        callback(new Error("请选择经纬度"));
      } else {
        callback();
      }
    };
    return {
      settleTypeShow: false,
      typeList: [],
      homeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务商信息表格数据
      providerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDialog: false,
      longitudeAndLatitude: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provName: null,
        provRegion: null,
        provAddr: null,
        liaison: null,
        phone: null,
        drawProportion: null,
        account: null,
        password: null,
        businessLicense: null,
        certificateCompetence: null,
        storeIcon: null,
        servType: null,
        servCategory: null,
        servGoods: null,
        isDel: null,
        isDis: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        provName: [
          {
            required: true,
            message: "服务商名称不能为空",
            trigger: "blur",
          },
        ],
        provRegion: [
          {
            required: true,
            message: "商户服务区不能为空",
            trigger: "blur",
          },
        ],
        provAddr: [
          {
            required: true,
            message: "商户地址不能为空",
            trigger: "blur",
          },
        ],
        longitudeAndLatitude: [
          { validator: validateLongitude, trigger: ["change", "blur"] },
        ],
        liaison: [
          {
            required: true,
            message: "联系人不能为空",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "联系电话不能为空",
            trigger: "blur",
          },
        ],
        drawProportion: [
          {
            required: true,
            message: "抽成比例  例如：0.05不能为空",
            trigger: "blur",
          },
        ],
        account: [
          {
            required: true,
            message: "登录账号不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "登录密码不能为空",
            trigger: "blur",
          },
        ],
        businessLicense: [
          {
            required: true,
            message: "营业执照不能为空",
            trigger: "blur",
          },
        ],
        certificateCompetence: [
          {
            required: true,
            message: "资格证书不能为空",
            trigger: "blur",
          },
        ],
        storeIcon: [
          {
            required: true,
            message: "店铺图片不能为空",
            trigger: "blur",
          },
        ],
        logo: [
          {
            required: true,
            message: "店铺详情banner不能为空",
            trigger: "blur",
          },
        ],
        servType: [
          {
            required: true,
            message: "服务类型",
            trigger: "change",
          },
        ],
        servCategory: [
          {
            required: true,
            message: "服务类目",
            trigger: "blur",
          },
        ],
        servGoods: [
          {
            required: true,
            message: "服务商品",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getTypes();
    this.getList();
  },
  methods: {
    /** 查询家政类型列列表 */
    getTypes() {
      this.loading = true;
      listType({
        pageSize: 9999,
      }).then((response) => {
        this.typeList = response.rows;
      });
    },
    /** 查询家政商品列表 */
    getHomeList(ids) {
      getProductbyids(ids).then((response) => {
        this.homeList = response.data;
        console.log(this.homeList);
      });
    },
    /** 服务类目发生改变 */
    servCategorychange(val) {
      this.form.servGoods = [];
      if (val.length == 0) {
        this.homeList = [];
      } else {
        this.getHomeList(val);
      }
    },
    /** 查询服务商信息列表 */
    getList() {
      this.loading = true;
      listProvider(this.queryParams).then((response) => {
        this.providerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        provName: null,
        provRegion: null,
        provAddr: null,
        liaison: null,
        phone: null,
        drawProportion: null,
        account: null,
        password: null,
        businessLicense: null,
        certificateCompetence: null,
        storeIcon: null,
        logo: null,
        servType: null,
        servCategory: null,
        servGoods: null,
        isDel: null,
        isDis: null,
        createTime: null,
      };
      this.resetForm("form");
      this.longitudeAndLatitude = ''
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务商信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProvider(id).then((response) => {
        this.form = response.data;
        this.form.servCategory = this.form.servCategory
          ? this.form.servCategory.split(",")
          : null;
        this.form.servGoods = this.form.servGoods
          ? this.form.servGoods.split(",")
          : null;
        this.longitudeAndLatitude = this.form.latitude + ',' + this.form.longitude
        this.form.password = "";
        this.open = true;
        this.title = "修改服务商信息";
        if (this.form.servType == 1) {
          this.getHomeList(this.form.servCategory);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const { servCategory, servGoods } = this.form;
          this.form.servCategory = servCategory ? servCategory.join(",") : null;
          this.form.servGoods = servGoods ? servGoods.join(",") : null;
          this.form.latitude = this.longitudeAndLatitude.split(",")[0]
          this.form.longitude = this.longitudeAndLatitude.split(",")[1]
          if (this.form.id != null) {
            updateProvider(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProvider(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除服务商信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProvider(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/provider/export",
        {
          ...this.queryParams,
        },
        `provider_${new Date().getTime()}.xlsx`
      );
    },
    //切换结算类型
    changeSettletype(item) {
      if (item == 2) {
        this.settleTypeShow = true;
      } else {
        this.settleTypeShow = false;
      }
    },
  },
};
</script>
